package Test.MyApp1;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

//import android.app.PendingIntent;
//import android.content.Intent;
//import android.telephony.SmsManager;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class ServerThread extends Thread {
	
	private final static String clsname="Act1"; 
	private static ServerThread _instance=null;
	
	/* (non-Javadoc)
	 * @see java.lang.Thread#destroy()
	 */
	@Override
	public void destroy() {
		// Auto-generated method stub
		Log.v(clsname,"Thread was destroied!");
		super.destroy();
	}

	private Handler _handler;
	private ServerThread()
	{
		
	}
	public static ServerThread getInstance(Handler _h)
	{
		synchronized(clsname)
		{
			if(_instance==null)
				_instance = new ServerThread();
			_instance._handler=_h;
			return _instance;
		}
		
	}
	

	public static String ByteToHex(byte a)
	{
		int aaa = (a < 0) ? a + 256 : a;
		StringBuilder sb = new StringBuilder();
		switch (aaa / 16)
		{
			case 10:
				sb.append("A");
				break;
			case 11:
				sb.append("B");
				break;
			case 12:
				sb.append("C");
				break;
			case 13:
				sb.append("D");
				break;
			case 14:
				sb.append("E");
				break;
			case 15:
				sb.append("F");
				break;
			default:
				sb.append(aaa / 16);
				break;
		}
		switch (aaa % 16)
		{
			case 10:
				sb.append("A");
				break;
			case 11:
				sb.append("B");
				break;
			case 12:
				sb.append("C");
				break;
			case 13:
				sb.append("D");
				break;
			case 14:
				sb.append("E");
				break;
			case 15:
				sb.append("F");
				break;
			default:
				sb.append(aaa % 16);
				break;
		}
		return sb.toString();
	}
		
	public static String toHex(byte[] data, int len)
	{
		String hexStr = null;
		if (data != null && len > 0)
		{
			StringBuilder strBuilder = new StringBuilder();

			for (int i = 0; i < len; i++)
			{
				strBuilder.append(ByteToHex(data[i]));
			}

			if (strBuilder != null && strBuilder.length() > 0)
			{
				hexStr = strBuilder.toString();
			}
		}
		return hexStr;
	}
	
	
	public void run()
	{
		Message msg=new Message();
		Log.v("Act1","Thread was started!");
		msg.obj="Thread was started!";
		_handler.sendMessage(msg);
		
		try {
			DatagramSocket sock=new DatagramSocket(12345);
			sock.setReuseAddress(true);
			sock.setBroadcast(true);
			//sock.bind(new InetSocketAddress("",12345));
			byte[] buffer=new byte[10000];
			DatagramPacket pack=new DatagramPacket(buffer,buffer.length);
			while(true)
			{
				sock.receive(pack);
				String s = new String(pack.getData(),0, pack.getLength());
				
				try{
					msg=new Message();
					msg.obj=s;
					InetAddress sa= pack.getAddress();
					Log.v(clsname, "Get data from :"+sa.toString() );
					sock.send(pack);
					_handler.sendMessage(msg);
				}
				catch(Exception e)
				{
					Log.v(clsname,"Exception @ recv:["+s+"]"+e.toString());
					msg=new Message();
					msg.obj="recv exception:"+e.toString();
					sock.send(pack);
					_handler.sendMessage(msg);
					continue;
				}
			} 
		} catch (SocketException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

		msg=new Message();
		msg.obj="Test Thread ended";
		Log.v(clsname,"Thread was over!");
		_handler.sendMessage(msg);
	}
}
